package com.sinopec.basemodule.model;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.sinopec.basemodule.view.PhotoPicker;

import java.io.File;

public class ImageBean implements Parcelable {
    private int type;
    /**
     * 本地资源id
     */
    private int resId = -1;
    private String label;
    private String localPath;
    private String netPath;
    private String relativePath;
    private String id;
    private PictureInfo pictureInfo;
    private String waterMarkPath;

    public ImageBean(int type, String label, String localPath, String netPath) {
        this.type = type;
        this.label = label;
        this.localPath = localPath;
        this.netPath = netPath;
    }

    public ImageBean(int type, String label, String localPath) {
        this.type = type;
        this.label = label;
        this.localPath = localPath;
    }


    public ImageBean(int type, String label) {
        this.type = type;
        this.label = label;
    }

    public ImageBean(int type) {
        this.type = type;
    }

    public ImageBean(String localPath) {
        this.localPath = localPath;
        this.type = PhotoPicker.TYPE_PHOTO;
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public String getNetPath() {
        return netPath;
    }

    public void setNetPath(String netPath) {
        this.netPath = netPath;
    }

    public String getRelativePath() {
        return relativePath;
    }

    public void setRelativePath(String relativePath) {
        this.relativePath = relativePath;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ImageBean)) return false;

        ImageBean imageBean = (ImageBean) o;

        if (localPath == null || imageBean.localPath == null) {//如果本地路径都为空，判断网络路径
            return (netPath != null || imageBean.netPath != null) && netPath != null && imageBean.netPath != null && netPath.equals(imageBean.netPath);
        } else {
            return localPath.equals(imageBean.localPath);
        }
    }

    @Override
    public int hashCode() {
        return localPath.hashCode();
    }

    public void setPictureInfo(PictureInfo pictureInfo) {
        this.pictureInfo = pictureInfo;
    }

    public PictureInfo getPictureInfo() {
        return pictureInfo;
    }


    public ImageInfoRequest getImageInfoRequest() {
        ImageInfoRequest imageInfoRequest = new ImageInfoRequest();
        imageInfoRequest.setBillPath(getNetPath());
        imageInfoRequest.setPicturePlace(pictureInfo.getPicAddress());
        imageInfoRequest.setPictureTime(pictureInfo.getPicDate() + " " + pictureInfo.getPicTime());
        return imageInfoRequest;
    }

    public void setWaterMarkPath(String waterMarkPath) {
        this.waterMarkPath = waterMarkPath;
    }

    public String getWaterMarkPath() {
        return waterMarkPath;
    }

    public Uri getImageUri() {
        Uri uri = null;
        if (!TextUtils.isEmpty(waterMarkPath)) {
            uri = Uri.fromFile(new File(waterMarkPath));
        }else if (!TextUtils.isEmpty(netPath)) {
            //uri = Uri.parse(AppConstants.IMAGE_SEVER + netPath);
            uri = Uri.parse(netPath);
        } else if (!TextUtils.isEmpty(localPath)) {
            uri = Uri.fromFile(new File(localPath));
        }
        return uri;
    }

    ///////////////////////////////////////////

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.type);
        dest.writeInt(this.resId);
        dest.writeString(this.label);
        dest.writeString(this.localPath);
        dest.writeString(this.netPath);
        dest.writeString(this.relativePath);
        dest.writeString(this.id);
        dest.writeParcelable(this.pictureInfo, flags);
        dest.writeString(this.waterMarkPath);
    }

    protected ImageBean(Parcel in) {
        this.type = in.readInt();
        this.resId = in.readInt();
        this.label = in.readString();
        this.localPath = in.readString();
        this.netPath = in.readString();
        this.relativePath = in.readString();
        this.id = in.readString();
        this.pictureInfo = in.readParcelable(PictureInfo.class.getClassLoader());
        this.waterMarkPath = in.readString();
    }

    public static final Creator<ImageBean> CREATOR = new Creator<ImageBean>() {
        @Override
        public ImageBean createFromParcel(Parcel source) {
            return new ImageBean(source);
        }

        @Override
        public ImageBean[] newArray(int size) {
            return new ImageBean[size];
        }
    };
}
